﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using BB.Apostas.Filters;
using BB.Apostas.Models;
using BB.Apostas.ViewModels;
using WebMatrix.WebData;
using System.Web.Caching;
using PagedList;

namespace BB.Apostas.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class ApostaController : Controller
    {
        private ApostasBalasDbContext db = new ApostasBalasDbContext();

        public ActionResult Index(int? page)
        {
            var competicaoactiva = db.Registos
                .Where(cr => cr.UserId == WebSecurity.CurrentUserId && cr.Active == true).SingleOrDefault();

            if (competicaoactiva == null)
                return View();

            ViewBag.Active = competicaoactiva.Competicao.Name;
            ViewBag.SectionIndex = "Index";
            int pageNumber = (page ?? 1);
            return View(db.Jornadas.Where(j => j.CompeticaoId == competicaoactiva.CompeticaoId)
                .OrderBy(j=>j.Id).ToPagedList(pageNumber, 10));
        }

        public ActionResult Edit(int id = 0)
        {
            var bets = new List<ApostaViewModel>();
            var jogos = db.Jogos.Where(j => j.JornadaId == id).OrderBy(j => j.Date).ToList();
            var totalscore = 0;
            foreach (var item in jogos)
            {
                var apostas = db.Apostas
                    .Where(a => a.UserId == WebSecurity.CurrentUserId && a.JogoId == item.Id)
                    .SingleOrDefault();
                var apostaviewmodel = new ApostaViewModel();

                apostaviewmodel.ApostaId = apostas == null ? 0 : apostas.Id;
                apostaviewmodel.AwayBet = apostas == null ? 0 : apostas.AwayScore;
                apostaviewmodel.HomeBet = apostas == null ? 0 : apostas.HomeScore;
                apostaviewmodel.Score = apostas == null ? 0 : apostas.Score;

                totalscore += apostaviewmodel.Score;

                apostaviewmodel.JogoId = item.Id;
                apostaviewmodel.AwayScore = item.AwayScore;
                apostaviewmodel.AwayTeam = item.AwayTeam;
                apostaviewmodel.Date = item.Date;
                apostaviewmodel.Fulfilled = item.Fulfilled;
                apostaviewmodel.HomeScore = item.HomeScore;
                apostaviewmodel.HomeTeam = item.HomeTeam;
                bets.Add(apostaviewmodel);
            }
            ViewBag.TotalScore = totalscore;
            return View(bets);
        }

        [HttpPost, ActionName("Apostar")]
        public ActionResult Apostar(IEnumerable<ApostaViewModel> apostas)
        {
            foreach (var item in apostas)
            {
                if (item.Fulfilled || item.Date <= DateTime.Now)
                    continue;

                if (item.ApostaId == 0)
                {
                    var aposta = new Aposta();
                    aposta.AwayScore = item.AwayBet;
                    aposta.HomeScore = item.HomeBet;
                    aposta.JogoId = item.JogoId;
                    aposta.UserId = WebSecurity.CurrentUserId;
                    db.Apostas.Add(aposta);
                }
                else
                {
                    var aposta = db.Apostas.Find(item.ApostaId);
                    aposta.AwayScore = item.AwayBet;
                    aposta.HomeScore = item.HomeBet;
                    db.Entry(aposta).State = EntityState.Modified;
                }
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }      
    }
}
